# -*- coding: utf-8 -*-
"""
-------------------------------------------------
Project Name: luffcity study project
File Name: 读取xml文件.py
Author: kij
Create Date: 2021/2/8
-------------------------------------------------
"""
import os
from xml.etree import ElementTree as ET


def get_file_path(file_name):
	return os.path.join(os.path.dirname(os.path.abspath(__file__)), "files", file_name)


file_path = get_file_path('xo.xml')
tree = ET.parse(file_path)
root = tree.getroot()
# for child in root:
# 	print(child.tag, child.attrib)
# 	for node in child:
# 		print(node.tag, node.attrib, node.text)

# for child in root.iter('year'):
# 	print(child.tag, child.attrib,child.text)

# v2 = root.find('country').find('rank')
# print(v2)
# v2 = root.findall('country')
# for node in v2:
# 	print(node.tag, node.attrib, node.text)
# # 修改节点
# v2 = root.find('country').find('rank')
# print(v2)
# v2.text = "7487"
# v2.set('update','2021-02-08')
# tree = ET.ElementTree(root)
# tree.write(get_file_path('new.xml'),encoding='utf-8')
# # 删除节点
# v2 = root.remove(root.find('country'))
# tree.write(get_file_path('newnew.xml'),encoding='utf-8')
# root = ET.Element("home")
# # 创建跟节点
# son1 = ET.Element('son',{'name':'erzi1'})
# son2 = ET.Element('son',{'name':'erzi2'})
# son3 = ET.Element('grandson',{'name':'孙子3'})
# son4 = ET.Element('grandson',{'name':'孙子4'})
# son5 = ET.Element('chonggrandson',{'name':'重孙子5'})
# son3.append(son5)
# son1.append(son3)
# son2.append(son4)
# root.append(son1)
# root.append(son2)
# tree = ET.ElementTree(root)
# # short_empty_elements  长标签或者端标签
# tree.write(get_file_path('sonxm.xml'),encoding='utf-8',short_empty_elements=False)


# root = ET.Element("famliy")
# # 创建跟节点
# son1 = root.makeelement('son',{'name':'erzi1'})
# son2 = root.makeelement('son',{'name':'erzi2'})
# son3 = root.makeelement('grandson',{'name':'孙子3'})
# son4 = root.makeelement('grandson',{'name':'孙子4'})
# son5 = root.makeelement('chonggrandson',{'name':'重孙子5'})
# son3.append(son5)
# son1.append(son3)
# son2.append(son4)
# root.append(son1)
# root.append(son2)
# tree = ET.ElementTree(root)
# # short_empty_elements  长标签或者端标签
# tree.write(get_file_path('newson.xml'),encoding='utf-8',short_empty_elements=False)


# root = ET.Element("love_famliy")
# # 创建跟节点
# son1 = ET.SubElement(root,'son',attrib={'name':'erzi1'})
# son2 = ET.SubElement(root,'son',attrib={'name':'erzi2'})
# son3 = ET.SubElement(son2,'grandson',attrib={'name':'孙子3'})
# son4 = ET.SubElement(son1,'grandson',attrib={'name':'孙子4'})
# son5 = ET.SubElement(son4,'chonggrandson',attrib={'name':'重孙子5'})
# son3.append(son5)
# son1.append(son3)
# son2.append(son4)
# root.append(son1)
# root.append(son2)
# tree = ET.ElementTree(root)
# # short_empty_elements  长标签或者端标签
# tree.write(get_file_path('lovenewson.xml'),encoding='utf-8',short_empty_elements=False)
#


